home *** CD-ROM | disk | FTP | other *** search
- //////////////////////////////////////////////////////////////////////
- // Pocket PC Game Programming
- //
- // CGameLibrary Source File
- //
- // This file includes the CGameLibrary class implementation.
- //
- //////////////////////////////////////////////////////////////////////
-
- #include "stdafx.h"
- #include "GameLibrary.h"
-
- CGameLibrary *CGameLibrary::pGameLib = NULL;
-
- //////////////////////////////////////////////////////////////////////
- // CGameLibrary::CGameLibrary constructor
- //
- //////////////////////////////////////////////////////////////////////
- CGameLibrary::CGameLibrary(HINSTANCE hInst, LPTSTR szNewWindowClass)
- {
- pGameLib = this;
- bHibernate = TRUE;
- bFullscreen = FALSE;
- hInstance = hInst;
- hWindow = NULL;
- iFrameRate = 60;
- bGAPIDisplay = FALSE;
-
- if (wcslen(szNewWindowClass) > 0)
- wcscpy(szWindowClass, szNewWindowClass);
- else
- wcscpy(szWindowClass, _T("<Unnamed Window Class>"));
-
- if (wcslen(szTitle) == 0)
- wcscpy(szTitle, _T("<Unnamed Title>"));
-
- }
-
- //////////////////////////////////////////////////////////////////////
- // CGameLibrary::~CGameLibrary destructor
- //
- //////////////////////////////////////////////////////////////////////
- CGameLibrary::~CGameLibrary()
- {
- }
-
- //////////////////////////////////////////////////////////////////////
- // CGameLibrary::Initialize
- // Performs both InitInstance and MyRegisterClass processes
- //////////////////////////////////////////////////////////////////////
- BOOL CGameLibrary::Initialize(int nCmdShow)
- {
- WNDCLASS wc;
- RECT rect;
-
- //If it is already running, then focus on the window
- hWindow = FindWindow(szWindowClass, szTitle);
- if (hWindow)
- {
- SetForegroundWindow ((HWND) (((DWORD)hWindow) | 0x01));
- return 0;
- }
-
- //Register the window class
- wc.style = CS_HREDRAW | CS_VREDRAW;
- wc.lpfnWndProc = (WNDPROC) WndProc;
- wc.cbClsExtra = 0;
- wc.cbWndExtra = 0;
- wc.hInstance = GetInstance();
- wc.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(GetIcon()));
- wc.hCursor = 0;
- wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
- wc.lpszMenuName = 0;
- wc.lpszClassName = szWindowClass;
-
- if (!RegisterClass(&wc))
- {
- Error(_T("Could not register window class!"));
- }
-
- GetClientRect(hWindow, &rect);
-
- if (GetFullscreen() || G_Enabled())
- {
- hWindow = CreateWindow(szWindowClass, szTitle,
- WS_VISIBLE, 0, 0,
- GetSystemMetrics(SM_CXSCREEN),
- GetSystemMetrics(SM_CYSCREEN),
- NULL, NULL, hInstance, NULL);
- } else {
- hWindow = CreateWindow(szWindowClass, szTitle,
- WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
- CW_USEDEFAULT, NULL, NULL, hInstance, NULL );
- }
- if (!hWindow)
- {
- Error(_T("Could not create the game window!"));
- return FALSE;
- }
-
- ShowWindow(hWindow, nCmdShow);
- UpdateWindow(hWindow);
-
- if (G_Enabled())
- {
- //initialize the GAPI display for fullscreen
- if (GXOpenDisplay(GetWindow(), GX_FULLSCREEN) == 0) {
- return FALSE;
- }
-
- //retrieve display properties
- gxDisplay = GXGetDisplayProperties();
-
- //make sure 16-bit color is available
- if ((GetBitsPerPixel() != 16) || (!IsScreenFormat565()))
- {
- Error(_T("Full 16-bit color display is required!"));
- GXCloseDisplay();
- return FALSE;
- }
- }
-
- //*** New code for Chapter 11 ********************************
-
- //set up buttons for exclusive control
- GXOpenInput();
- gxKeys = GXGetDefaultKeys(GX_NORMALKEYS);
-
- //************************************************************
-
- return TRUE;
- }
-
- //////////////////////////////////////////////////////////////////////
- // CGameLibrary::EventHandler
- // Handles events passed from WndProc
- //////////////////////////////////////////////////////////////////////
- LRESULT CGameLibrary::EventHandler(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
- {
-
- //*** 02/11 for chapter 11 ***************************************************
- short vkKey;
- POINT pt;
- BOOL bButton;
-
- switch (message)
- {
- case WM_CREATE:
- SetWindow(hWnd);
- SetForegroundWindow(GetWindow());
- GameStart(hWnd);
- break;
-
- case WM_HIBERNATE:
- SetHibernate(TRUE);
- SHFullScreen(GetWindow(), SHFS_SHOWSTARTICON|SHFS_SHOWTASKBAR|SHFS_SHOWSIPBUTTON);
- break;
-
- case WM_KILLFOCUS:
- SetHibernate(TRUE);
- if (G_Enabled())
- GXSuspend();
- break;
-
- case WM_ACTIVATE:
- GameActivate(hWnd);
- if (GetHibernate())
- {
- SetHibernate(FALSE);
- }
- if (G_Enabled())
- GXResume();
- else
- if (GetFullscreen())
- {
- if( wParam == WA_INACTIVE )
- SHFullScreen(hWnd, SHFS_SHOWSTARTICON|SHFS_SHOWTASKBAR|SHFS_SHOWSIPBUTTON);
- else
- SHFullScreen(hWnd, SHFS_HIDESTARTICON|SHFS_HIDETASKBAR|SHFS_HIDESIPBUTTON);
- }
-
- break;
-
- case WM_PAINT:
- GamePaint(hWnd);
- break;
-
- case WM_LBUTTONDOWN:
- StylusDown(LOWORD(lParam), HIWORD(lParam));
- break;
-
- case WM_MOUSEMOVE:
- StylusMove(LOWORD(lParam), HIWORD(lParam));
- break;
-
- case WM_LBUTTONUP:
- StylusUp(LOWORD(lParam), HIWORD(lParam));
- break;
-
- //*** 02/11 for chapter 11 *******************************************************
- case WM_KEYDOWN:
- vkKey = (short)wParam;
- bButton = TRUE;
-
- if (vkKey == gxKeys.vkUp)
- {
- pt = gxKeys.ptUp;
- vkKey = 0;
- }
-
- else if (vkKey == gxKeys.vkDown)
- {
- pt = gxKeys.ptDown;
- vkKey = 1;
- }
-
- else if (vkKey == gxKeys.vkLeft)
- {
- pt = gxKeys.ptLeft;
- vkKey = 2;
- }
-
- else if (vkKey == gxKeys.vkRight)
- {
- pt = gxKeys.ptRight;
- vkKey = 3;
- }
-
- else if (vkKey == gxKeys.vkA)
- {
- pt = gxKeys.ptA;
- vkKey = 4;
- }
-
- else if (vkKey == gxKeys.vkB)
- {
- pt = gxKeys.ptB;
- vkKey = 5;
- }
-
- else if (vkKey == gxKeys.vkC)
- {
- pt = gxKeys.ptC;
- vkKey = 6;
- }
-
- else if (vkKey == gxKeys.vkStart)
- {
- pt = gxKeys.ptStart;
- vkKey = 7;
- }
-
- else if (vkKey == 192)
- {
- pt = gxKeys.ptStart;
- vkKey = 8;
- }
-
- else if (vkKey == 193)
- {
- pt = gxKeys.ptStart;
- vkKey = 9;
- }
-
- else
- bButton = FALSE;
-
- if (bButton)
- ButtonPress(vkKey, pt);
-
- break;
-
- //*** 02/11 for chapter 11 *******************************************************
- case WM_KEYUP:
- vkKey = (short)wParam;
- bButton = TRUE;
-
- if (vkKey == gxKeys.vkUp)
- {
- pt = gxKeys.ptUp;
- vkKey = 0;
- }
-
- else if (vkKey == gxKeys.vkDown)
- {
- pt = gxKeys.ptDown;
- vkKey = 1;
- }
-
- else if (vkKey == gxKeys.vkLeft)
- {
- pt = gxKeys.ptLeft;
- vkKey = 2;
- }
-
- else if (vkKey == gxKeys.vkRight)
- {
- pt = gxKeys.ptRight;
- vkKey = 3;
- }
-
- else if (vkKey == gxKeys.vkA)
- {
- pt = gxKeys.ptA;
- vkKey = 4;
- }
-
- else if (vkKey == gxKeys.vkB)
- {
- pt = gxKeys.ptB;
- vkKey = 5;
- }
-
- else if (vkKey == gxKeys.vkC)
- {
- pt = gxKeys.ptC;
- vkKey = 6;
- }
-
- else if (vkKey == gxKeys.vkStart)
- {
- pt = gxKeys.ptStart;
- vkKey = 7;
- }
-
- else if (vkKey == 192)
- {
- pt = gxKeys.ptStart;
- vkKey = 8;
- }
-
- else if (vkKey == 193)
- {
- pt = gxKeys.ptStart;
- vkKey = 9;
- }
-
- else
- bButton = FALSE;
-
- if (bButton)
- ButtonRelease(vkKey, pt);
-
- break;
-
- case WM_DESTROY:
- GameEnd();
- if (G_Enabled())
- GXCloseDisplay();
-
- //*** 02/11 for chapter 11 *******************************************************
- GXCloseInput();
-
- PostQuitMessage(0);
- break;
-
- default:
- return DefWindowProc(hWnd, message, wParam, lParam);
-
- }
- return 0;
- }
-
- //////////////////////////////////////////////////////////////////////
- // CGameLibrary::Error
- // Handles error messages for the library
- //////////////////////////////////////////////////////////////////////
- void CGameLibrary::Error(LPTSTR szError)
- {
- TCHAR szMsg[100];
-
- wsprintf(szMsg, _T("%s:\n%s\n%s, %i"), szMsg, __FILE__, __LINE__);
- MsgBox(szMsg);
- }
-
-
- //////////////////////////////////////////////////////////////////////
- // CGameLibrary::Shutdown
- // Sends the close window message to end the program
- //////////////////////////////////////////////////////////////////////
- void CGameLibrary::Shutdown()
- {
- PostMessage(hWindow, WM_CLOSE, 0, 0);
- }
-
- //////////////////////////////////////////////////////////////////////
- // CGameLibrary::G_BeginDraw
- // Sets up the GAPI for drawing to video memory.
- //////////////////////////////////////////////////////////////////////
- void CGameLibrary::G_BeginDraw()
- {
- if (G_Enabled())
- GAPIVidMem = (unsigned char *)GXBeginDraw();
- }
-
- //////////////////////////////////////////////////////////////////////
- // CGameLibrary::G_EndDraw
- // End the draw session and kill the video memory pointer.
- //////////////////////////////////////////////////////////////////////
- void CGameLibrary::G_EndDraw()
- {
- if (G_Enabled())
- GXEndDraw();
- }
-
- ////////////////////////////////////////////////////////////
- // FUNCTION: G_ClearScreen
- //
- // PURPOSE: Clears the GAPI screen using specified color
- //
- BOOL CGameLibrary::G_ClearScreen(COLOR color)
- {
- G_BeginDraw();
- for (int y = 0; y < ScreenHeight(); y++) {
- for (int x = 0; x < ScreenWidth(); x++) {
- G_DrawPixel16(G_GetVidMem(), x, y, color);
- }
- }
- G_EndDraw();
- return TRUE;
- }
-
- ////////////////////////////////////////////////////////////
- // FUNCTION: G_DrawPixel16
- //
- // PURPOSE: Draw pixel on a 16-bit display.
- //
- int CGameLibrary::G_DrawPixel16(unsigned char *VidMem, int X, int Y, COLOR color)
- {
- unsigned short usColor;
- int address;
-
- //find pixel location in video memory
- address = (X * GetXPitch()) + (Y * GetYPitch());
-
- //set color bits for 565 packed format
- usColor = (unsigned short)
- (((color.Red & 0xf8) << 8) |
- ((color.Green & 0xfc) << 3) |
- ((color.Blue & 0xf8) >> 3) );
-
- //display pixel
- *(unsigned short *)(VidMem + address) = usColor;
-
- return 0;
- }
-
- ////////////////////////////////////////////////////////////
- // FUNCTION: G_DrawSolidRect
- //
- // PURPOSE: Draws a solid rectangle using GAPI video memory.
- //
- void CGameLibrary::G_DrawSolidRect(unsigned char *VidMem, int iLeft, int iTop, int iRight, int iBottom, COLOR color)
- {
- int x, y;
-
- for (y = iTop; y<=iBottom; y++)
- {
- for (x = iLeft; x<=iRight; x++)
- {
- G_DrawPixel16(VidMem, x, y, color);
- }
- }
- }
-
- //////////////////////////////////////////////////////////////////////
- // FatalError
- // Displays error message and then ends program.
- //////////////////////////////////////////////////////////////////////
- void CGameLibrary::FatalError(LPTSTR lpStr)
- {
- MessageBox(GetWindow(), lpStr, _T("Fatal Error"), MB_OK | MB_ICONERROR);
- Shutdown();
- }
-
-
- //////////////////////////////////////////////////////////////////////
- // PrintText
- // Prints a Unicode string to the screen coordinates x,y
- //////////////////////////////////////////////////////////////////////
- void CGameLibrary::PrintText(HDC hdc, LPCTSTR strText, int x, int y, COLORREF color, int iBkMode)
- {
- static TEXTMETRIC tm;
- static int cxChar, cyChar, cxCaps;
- static BOOL bFirst = TRUE;
-
- if (bFirst)
- {
- GetTextMetrics(hdc, &tm);
- cxChar = tm.tmAveCharWidth;
- cyChar = tm.tmHeight + tm.tmExternalLeading;
- cxCaps = (tm.tmPitchAndFamily & 1 ? 3 : 2) * cxChar / 2;
- bFirst = FALSE;
- }
-
- SetTextColor(hdc, color);
- SetBkMode(hdc, iBkMode);
- ExtTextOut(hdc, x + cxChar, cyChar * y, 0, NULL, strText, wcslen(strText), NULL);
- }
-
- //////////////////////////////////////////////////////////////////////
- // GetPath
- // Returns full pathname of a file in the current directory.
- //////////////////////////////////////////////////////////////////////
- LPWSTR CGameLibrary::GetPath(TCHAR *filename)
- {
- TCHAR *lpFilename;
- TCHAR *pDest;
- int nStrLen;
- int result;
- int ch = '\\';
-
- lpFilename = new TCHAR[255];
- nStrLen = GetModuleFileName(NULL, lpFilename, 255);
-
- pDest = wcsrchr(lpFilename, ch);
- if (pDest != NULL)
- {
- result = pDest - lpFilename + 1;
- lpFilename[result] = '\0';
-
- if (filename != NULL)
- wcscat(lpFilename, filename);
-
- return lpFilename;
- }
- else
- return NULL;
- }
-
- //////////////////////////////////////////////////////////////////////
- // WndProc
- // Primary window callback function
- //////////////////////////////////////////////////////////////////////
- LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
- {
- return GameLib()->EventHandler(hWnd, message, wParam, lParam);
- }
-
- //////////////////////////////////////////////////////////////////////
- // WinMain
- // Main entry point of program (library)
- //////////////////////////////////////////////////////////////////////
- int WINAPI WinMain( HINSTANCE hInstance,
- HINSTANCE hPrevInstance,
- LPTSTR lpCmdLine,
- int nCmdShow )
- {
- MSG msg;
- static iNewTime = 0;
- int iTickCount;
-
- if (GameInit(hInstance))
- {
-
- if (!GameLib()->Initialize(nCmdShow))
- {
- return FALSE;
- }
-
- // Main message loop:
- while (TRUE)
- {
- __try
- {
- if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
- {
- if( msg.message == WM_QUIT )
- break;
-
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- else
- {
- if (!GameLib()->GetHibernate())
- {
- iTickCount = GetTickCount();
- if (iTickCount > iNewTime)
- {
- iNewTime = iTickCount + GameLib()->GetFrameRate();
- GameEvent();
- }
- }
- }
- }
- __except(EXCEPTION_EXECUTE_HANDLER)
- {
- MessageBox(NULL, _T("Exception error!"), _T("Error"), MB_OK | MB_ICONERROR);
- }
- }
-
- return msg.wParam;
- }
-
- GameEnd();
- return 1;
- }
-
- //////////////////////////////////////////////////////////////////////
- // MsgBox
- // Displays a pop-up message box
- //////////////////////////////////////////////////////////////////////
- void MsgBox(LPTSTR szMessage)
- {
- MessageBox(GetForegroundWindow(), szMessage, GameLib()->szTitle, MB_OK | MB_ICONINFORMATION);
- }
-
-